// zh_gjw/pages/admin/admin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
		izcus: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
		if (wx.getStorageSync('adminlogin')){
			that.setData({
				admin: wx.getStorageSync('adminlogin'),
				password: wx.getStorageSync('adminpassword')
			})
		}
		getApp().location_attachurl(this)
		getApp().location_url(this)
    getApp().util.request({
      'url': 'entry/wxapp/logo',
      'cachetime': '0',
      success: function (res) {
        that.setData({
          logo: res.data.res
        })
      }
    });
    getApp().util.request({
      'url': 'entry/wxapp/sertel',
      'cachetime': '0',
      success: function (res) {
        that.setData({
          sertel: res.data.res
        })
      }
		});
		getApp().util.request({//好像没什么用
			'url': 'entry/wxapp/Iscustomers',
			'cachetime': '0',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if(res.data.code==200 || res.data.code==204){
					that.setData({
						izcus: 1
					})
				}
			}
		});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
	admin:function(e){
		this.setData({
			admin: e.detail.value
		})
	},
	password: function (e) {
		this.setData({
			password:e.detail.value
		})
	},
  login: function () {
    var that = this
    getApp().util.request({
      'url': 'entry/wxapp/Cusperson',
      'cachetime': '0',
      data: { admin:that.data.admin},
      success: function (reso) {
        if (reso.data.code == 500) {
          wx.showToast({
            icon: "none",
            title: '不存在该商家',
          })
        } else if (reso.data.code == 200) {
					if (reso.data.res.type == 0) {//普通商家
            getApp().util.request({
              url: 'entry/wxapp/Cuspaydate',
							data: { admin: that.data.admin},
              success: function (resoo) {
                if (resoo.data.code == 200) {
                  getApp().util.request({
                    'url': 'entry/wxapp/cuslogin',
                    'cachetime': '0',
                    data: { admin: that.data.admin, password: that.data.password },
                    success: function (res) {
                      if (res.data.code == 201) {
                        wx.showToast({
                          icon: "none",
                          title: res.data.msg,
                        })
                      }
                      if (res.data.code == 500) {
                        wx.showToast({
                          icon: "none",
                          title: res.data.msg,
                        })
                      }
                      if (res.data.code == 200) {
                        wx.showToast({
                          icon: "none",
                          title: res.data.msg,
                        })
                        wx.setStorageSync('adminlogin', that.data.admin)
                        wx.setStorageSync('adminpassword', that.data.password)
                        wx.navigateTo({
                          url: '/zh_gjw/pages/cuscenter/cuscenter?admin=' + that.data.admin,
                        })
                      }
                    }
                  });
                } else if (resoo.data.code == 201) {
                  getApp().util.request({
                    url: 'entry/wxapp/Cuspayagain',
                    data: { openid: wx.getStorageSync('openid'), publishfee: that.data.publishfee.publishfee },
                    success: function (sres) {
                      wx.requestPayment({
                        'timeStamp': sres.data.timeStamp,
                        'nonceStr': sres.data.nonceStr,
                        'package': sres.data.package,
                        'signType': 'MD5',
                        'paySign': sres.data.paySign,
                        'success': function (pres) {
                          getApp().util.request({
                            url: 'entry/wxapp/Cuspaydatesuccess',
                            data: { openid: wx.getStorageSync('openid') },
                            success: function (stres) {
                              if (stres.data.code == 200) {

                                getApp().util.request({
                                  'url': 'entry/wxapp/cuslogin',
                                  'cachetime': '0',
                                  data: { admin: that.data.admin, password: that.data.password },
                                  success: function (res) {
                                    if (res.data.code == 201) {
                                      wx.showToast({
                                        icon: "none",
                                        title: res.data.msg,
                                      })
                                    }
                                    if (res.data.code == 500) {
                                      wx.showToast({
                                        icon: "none",
                                        title: res.data.msg,
                                      })
                                    }
                                    if (res.data.code == 200) {
                                      wx.showToast({
                                        icon: "none",
                                        title: res.data.msg,
                                      })
                                      wx.setStorageSync('adminlogin', that.data.admin)
                                      wx.setStorageSync('adminpassword', that.data.password)
                                      wx.navigateTo({
                                        url: '/zh_gjw/pages/cuscenter/cuscenter?admin=' + that.data.admin,
                                      })
                                    }
                                  }
                                });
                              }
                            }
                          })
                        },
                        'fail': function (pres) {
                          wx.showToast({
                            title: '支付失败',
                            icon: "loading",
                            duration: 1000
                          })
                        }
                      })
                    }
                  })
                }
              }
            })
          } else if (reso.data.res.type == 1) {//定制商家
            getApp().util.request({
              url: 'entry/wxapp/Cuspaydate',
							data: { admin: that.data.admin },
              success: function (resoo) {
                if (resoo.data.code == 200) {
                  getApp().util.request({
                    'url': 'entry/wxapp/cuslogin',
                    'cachetime': '0',
                    data: { admin: that.data.admin, password: that.data.password },
                    success: function (res) {
                      if (res.data.code == 201) {
                        wx.showToast({
                          icon: "none",
                          title: res.data.msg,
                        })
                      }
                      if (res.data.code == 500) {
                        wx.showToast({
                          icon: "none",
                          title: res.data.msg,
                        })
                      }
                      if (res.data.code == 200) {
                        wx.showToast({
                          icon: "none",
                          title: res.data.msg,
                        })
                        wx.setStorageSync('adminlogin', that.data.admin)
                        wx.setStorageSync('adminpassword', that.data.password)
                        wx.navigateTo({
                          url: '/zh_gjw/pages/cuscenter/cuscenter?admin=' + that.data.admin,
                        })
                      }
                    }
                  });
                } else if (resoo.data.code == 201) {
                  getApp().util.request({
                    url: 'entry/wxapp/Cuspayagain',
										data: { openid: wx.getStorageSync("openid"), publishfee: that.data.publishfee.dpublishfee },
                    success: function (sres) {
                      wx.requestPayment({
                        'timeStamp': sres.data.timeStamp,
                        'nonceStr': sres.data.nonceStr,
                        'package': sres.data.package,
                        'signType': 'MD5',
                        'paySign': sres.data.paySign,
                        'success': function (pres) {
                          getApp().util.request({
                            url: 'entry/wxapp/Cuspaydatesuccess',
														data: { openid: wx.getStorageSync("openid") },
                            success: function (stres) {
                              if (stres.data.code == 200) {

                                getApp().util.request({
                                  'url': 'entry/wxapp/cuslogin',
                                  'cachetime': '0',
                                  data: { admin: that.data.admin, password: that.data.password },
                                  success: function (res) {
                                    if (res.data.code == 201) {
                                      wx.showToast({
                                        icon: "none",
                                        title: res.data.msg,
                                      })
                                    }
                                    if (res.data.code == 500) {
                                      wx.showToast({
                                        icon: "none",
                                        title: res.data.msg,
                                      })
                                    }
                                    if (res.data.code == 200) {
                                      wx.showToast({
                                        icon: "none",
                                        title: res.data.msg,
                                      })
                                      wx.setStorageSync('adminlogin', that.data.admin)
                                      wx.setStorageSync('adminpassword', that.data.password)
                                      wx.navigateTo({
                                        url: '/zh_gjw/pages/cuscenter/cuscenter?admin=' + that.data.admin,
                                      })
                                    }
                                  }
                                });
                              }
                            }
                          })
                        },
                        'fail': function (pres) {
                          wx.showToast({
                            title: '支付失败',
                            icon: "loading",
                            duration: 1000
                          })
                        }
                      })
                    }
                  })
                }
              }
            })
          }
        }
      }
    });


  },
})